package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.api.RemoteUserService;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.mapper.EntMeetSignMapper;
import com.prd.model.EntMeetSign;
import com.prd.model.LoginUser;
import com.prd.service.EntMeetSignService;
import com.prd.util.StringsUtils;
import com.prd.vo.EntMeetSignVo;
import com.prd.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EntMeetSignServiceImpl extends ServiceImpl<EntMeetSignMapper, EntMeetSign> implements EntMeetSignService {

    @Autowired
    private EntMeetSignMapper entMeetSignMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public int insertInfo(Long meetId, Integer status) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntMeetSign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntMeetSign::getMeetId, meetId);
        queryWrapper.eq(EntMeetSign::getCreateId, loginUser.getId());
        queryWrapper.eq(EntMeetSign::getStatus, status);
        List<EntMeetSign> list = entMeetSignMapper.selectList(queryWrapper);
        if (list != null) {
            throw new RuntimeException("你已操作了！");
        }
        EntMeetSign entMeetSign = new EntMeetSign();
        entMeetSign.setMeetId(meetId);
        entMeetSign.setStatus(status);
        entMeetSign.setIsCancel(2);
        entMeetSign.setCreateId(loginUser.getId());
        entMeetSign.setCreateTime(new Date());
        return entMeetSignMapper.insert(entMeetSign);
    }

    @Override
    public int insertLeaveInfo(EntMeetSign entMeetSign) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntMeetSign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntMeetSign::getMeetId, entMeetSign.getMeetId());
        queryWrapper.eq(EntMeetSign::getCreateId, loginUser.getId());
        queryWrapper.eq(EntMeetSign::getStatus, entMeetSign.getStatus());
        List<EntMeetSign> list = entMeetSignMapper.selectList(queryWrapper);
        if (list != null) {
            throw new RuntimeException("你已提交了！");
        }
        entMeetSign.setLeaveNumber(String.valueOf(System.currentTimeMillis()));
        entMeetSign.setLeaveStatus(CheckStatus.ONE.getCode());
        entMeetSign.setCreateId(loginUser.getId());
        entMeetSign.setCreateTime(new Date());
        return entMeetSignMapper.insert(entMeetSign);
    }

    @Override
    public int updateInfo(EntMeetSign entMeetSign) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        entMeetSign.setUpdateId(loginUser.getId());
        entMeetSign.setUpdateTime(new Date());
        return entMeetSignMapper.updateById(entMeetSign);
    }

    @Override
    public int cancel(Long id) {
        EntMeetSign entMeetSign = entMeetSignMapper.selectById(id);
        if (entMeetSign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entMeetSign.setIsCancel(1);
        return updateInfo(entMeetSign);
    }

    @Override
    public EntMeetSignVo details(Long meetId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntMeetSign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntMeetSign::getMeetId, meetId);
        queryWrapper.eq(EntMeetSign::getCreateId, loginUser.getId());
        queryWrapper.eq(EntMeetSign::getStatus, 4);
        EntMeetSign entMeetSign = entMeetSignMapper.selectOne(queryWrapper);
        if (entMeetSign == null) {
            throw new RuntimeException("记录没有找到！");
        }
        List<Long> arrayList = new ArrayList<>();
        arrayList.add(entMeetSign.getCreateId());
        arrayList.add(entMeetSign.getLeaveApprove());
        EntMeetSignVo entMeetSignVo = new EntMeetSignVo();
        BeanUtils.copyProperties(entMeetSign, entMeetSignVo);
        List<UserVo> listUser = remoteUserService.listInfo(arrayList.toArray(new Long[arrayList.size()])).getData();
        for (UserVo userVo : listUser) {
            if (entMeetSign.getCreateId() == userVo.getId()) {
                entMeetSignVo.setCreateIdStr(userVo.getName());
            }
            if (entMeetSign.getLeaveApprove() == userVo.getId()) {
                entMeetSignVo.setLeaveApproveStr(userVo.getName());
            }
        }
        return entMeetSignVo;
    }

}
